package by.grsu.shedule.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import by.grsu.shedule.constants.Week;
import by.grsu.shedule.fragment.DateFragment;

import android.util.MonthDisplayHelper;

public class CalendarHelper {

	public static Date getThisStartWeek() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return new Date(calendar.get(Calendar.YEAR) - 1900,
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
	}

	public static Date getThisFinishWeek() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return new Date(calendar.get(Calendar.YEAR) - 1900,
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
	}

	public static Date getNextStartWeek() {
		MonthDisplayHelper displayHelper = new MonthDisplayHelper(Calendar
				.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(
				Calendar.MONTH));
		int dayInMonth = displayHelper.getNumberOfDaysInMonth();
		if (getThisFinishWeek().getDate() + 1 > dayInMonth) {
			if (getThisFinishWeek().getMonth() == 12) {
				return new Date(getThisFinishWeek().getYear() + 1, 1, 1);
			}

			return new Date(getThisFinishWeek().getYear(), getThisFinishWeek()
					.getMonth() + 1, 1);

		}

		return new Date(getThisFinishWeek().getYear(), getThisFinishWeek()
				.getMonth(), getThisFinishWeek().getDate() + 1);
	}

	public static Date getNextFinishWeek() {
		MonthDisplayHelper displayHelper = new MonthDisplayHelper(Calendar
				.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(
				Calendar.MONTH));
		int dayInMonth = displayHelper.getNumberOfDaysInMonth();
		if (getThisFinishWeek().getDate() + 8 > dayInMonth) {
			if (getThisFinishWeek().getMonth() == 12) {
				return new Date(getThisFinishWeek().getYear() + 1, 1,
						getThisFinishWeek().getDate() - dayInMonth + 7);
			}

			return new Date(getThisFinishWeek().getYear(), getThisFinishWeek()
					.getMonth() + 1, getThisFinishWeek().getDate() - dayInMonth
					+ 7);

		}

		return new Date(getThisFinishWeek().getYear(), getThisFinishWeek()
				.getMonth(), getThisFinishWeek().getDate() + 7);

	}

	public static String convertDateForDateFragment(Date dateStartWeek,
			Date dateFinishWeek) {
		SimpleDateFormat sdfOut = new SimpleDateFormat("dd MMMMM");
		String result = sdfOut.format(dateStartWeek) + " - "
				+ sdfOut.format(dateFinishWeek);
		return result;
	}

	public static String convertDateForLoock(Date dateStartWeek, int dayNumber) {
		SimpleDateFormat sdfOut = new SimpleDateFormat("dd MMMMM");
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateStartWeek);
		cal.add(Calendar.DATE, dayNumber);
		return sdfOut.format(cal.getTime());
	}

	public static String convertDateForSearch(Date dateStartWeek) {
		SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateStartWeek);
		return sdfOut.format(cal.getTime());
	}

	public static String convertDateForSearch(Date dateStartWeek, int dayNumber) {
		SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateStartWeek);
		cal.add(Calendar.DATE, dayNumber);
		return sdfOut.format(cal.getTime());
	}

	// public static List<Date> getCurrentDateList(Date dateStartWeek) {
	// List<Date> resultList = new ArrayList<Date>();
	// Calendar cal = Calendar.getInstance();
	// cal.setTime(dateStartWeek);
	// for (int i = 0; i < 7; i++) {
	// if (i == 0) {
	// resultList.add(cal.getTime());
	// } else {
	// cal.add(Calendar.DATE, 1);
	// resultList.add(cal.getTime());
	// }
	// }
	// return resultList;
	//
	// }

	public static List<String> getCurrentDateList() {
		List<String> resultList = new ArrayList<String>();
		Date dateStartWeek = DateFragment.getCurrentWeek();
		for (int i = 0; i < 7; i++) {
			resultList.add(CalendarHelper
					.convertDateForSearch(dateStartWeek, i));
		}
		return resultList;
	}

	public static int getCurrentDayNumber(Date date) {
		int result = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

		if (result == 1) {
			result = 6;
		} else {
			result = result - 2;
		}

		if (result >= 0 && result <= 6) {
			return result;
		}
		return 0;
	}

	public static Week getWeek(Date date) {

		if (date.getTime() < getNextStartWeek().getTime()) {
			return Week.THIS;
		}

		return Week.NEXT;
	}
}
